﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;
using CRMEntity;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for ApplicationInfo
    /// </summary>
    public class ApplicationInfo : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }


        // Get data From app_info for Application
        public void GetApplicationDetail()
        {
            DataSet ds = new DataSet();
            try
            {
                int input_app_id = string.IsNullOrEmpty(context.Request.QueryString["app_id"]) ? 0 : int.Parse(context.Request.QueryString["app_id"]);
                ds = ApplicationListHandler.GetApplicationDetail(input_app_id);
                context.Response.Write(JsonConvert.SerializeObject(ds.Tables[0]));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }


        public void SelectApplicationType(int app_id)
        {
            try
            {
                context.Response.Write(ApplicationDetailHandler.selectApplicationType(app_id));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }

        }
        public void deleteApplication(int app_id)
        {
            try
            {
                context.Response.Write(ApplicationDetailHandler.DeleteSingleApplication(app_id));
            }
            catch (Exception ex)
            {
                context.Response.Write("0");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }
        public void deleteMultipleApplications(string applications)
        {
            bool flag = false;
            if (!string.IsNullOrEmpty(applications))
            {
                try
                {
                    flag = ApplicationDetailHandler.DeleteMultipleApplication(applications);
                    context.Response.Write(flag);
                }
                catch (Exception ex)
                {
                    ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
                }
            }
        }
        public void saveNewApplicationProfile(Int32 ContactID, int NoInApplication, string CombineApp, string DesiredAmount, string OfferedAmount, string app_result, string LoadNo, int NoInApplications, string Folder, string amountMoto, string amoutProperties, string TextAwaitDoc, string purposeSummary, string LoanStatus, string memo, string outSrcLenderVal, string referrerVal, string Operator, int IsCarOwner)
        {

            DataSet ds;
            try
            {
                Application objApp = new Application();
                objApp.ContactID = ContactID;
                objApp.NoInApplication = NoInApplication;
                objApp.CombineApp = CombineApp;
                objApp.DesiredAmount = DesiredAmount;
                objApp.OfferedAmount = OfferedAmount;
                objApp.app_result = app_result;
                objApp.LoadNo = LoadNo;
                objApp.NoInApplications = NoInApplications;
                objApp.Folder = Folder;
                objApp.amountMoto = amountMoto;
                objApp.amoutProperties = amoutProperties;
                objApp.TextAwaitDoc = TextAwaitDoc;
                objApp.PurposeSummary = purposeSummary;
                objApp.Memo = memo;
                objApp.OutSrcLenderVal = outSrcLenderVal;
                objApp.ReferrerVal = referrerVal;
                objApp.IsCarOwner = IsCarOwner;
                objApp.Operator = Operator;
                objApp.LoanStatus = LoanStatus;


                ds = objApp.AddNewApplicationProfile();
                context.Response.Write(JsonConvert.SerializeObject(ds.Tables[0]));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }

        }
        public void UpdateApplicationProfile(Int32 appid, string desiredAmount, string offeredAmount, string resultDescription, string loadNo, string folder,
            string motorCount, string propertyCount, string TextAwaitDoc, string purposeSummary, string memo, string outSrcLenderVal, string referrerVal, string oprator, int IsCarOwner, string LoanStatus)
        {
            bool response = false;
            try
            {
                Application objApp = new Application();
                objApp.app_id = appid;
                objApp.DesiredAmount = desiredAmount;
                objApp.OfferedAmount = offeredAmount;
                objApp.app_result = resultDescription; ;
                objApp.LoadNo = loadNo;
                objApp.Folder = folder;
                objApp.amountMoto = motorCount;
                objApp.amoutProperties = propertyCount;
                objApp.TextAwaitDoc = TextAwaitDoc;
                objApp.PurposeSummary = purposeSummary;
                objApp.Memo = memo;
                objApp.OutSrcLenderVal = outSrcLenderVal;
                objApp.ReferrerVal = referrerVal;
                objApp.IsCarOwner = IsCarOwner;
                objApp.LoanStatus = LoanStatus;
                objApp.Operator = oprator;

                response = objApp.UpdateAppDetailData();
                context.Response.Write(response);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }

        }
        public void BindApplicationForSMSFields(int app_id)
        {
            try
            {
                DataTable dt = ApplicationDetailHandler.BindApplicationForSMSFields(app_id);
                string strJson = JsonConvert.SerializeObject(dt);

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void SelectApplicationProfileData(int app_id, string OperatorID)
        {
            DataTable dt;
            try
            {
                dt = ClientProfileHandler.BindDataTableForApplication(app_id, OperatorID);
                context.Response.Write(JsonConvert.SerializeObject(dt));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }
        public void SelectAllApplicants(int app_id, int contact_id)
        {
            DataTable dt;
            try
            {
                dt = ClientProfileHandler.GetApplicantList(app_id, contact_id);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }
        public void BindApplicationForm(int app_id)
        {

            try
            {
                //var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                //var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                var input_app_id = string.IsNullOrEmpty(context.Request["app_id"]) ? 0 : int.Parse(context.Request["app_id"]);
                context.Response.Write(JsonConvert.SerializeObject(ClientProfileHandler.BindAppForms(input_app_id)));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void RemoveDuplicateApp(int ApplicationId, int AppFormId)
        {
            bool flag = false;
            try
            {
                ClientProfileHandler.RemoveDuplicateData(ApplicationId, AppFormId);
                flag = true;
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            context.Response.Write(flag);
        }

        public void ShowAppNames(int app_id)
        {
            DataTable dt;
            try
            {
                if (app_id > 0)
                {
                    dt = ClientProfileHandler.GetApplicantNames(app_id);
                    context.Response.Write(JsonConvert.SerializeObject(dt));
                }
                else
                    context.Response.Write("0");
            }
            catch (Exception ex)
            {
                context.Response.Write("1");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void IsValidContactForCombine(int app_id, int contactid)
        {
            try
            {
                context.Response.Write(ApplicationDetailHandler.isValidContact(app_id, contactid));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        
      /// <summary>
      /// Get contact id list by name --from Wade 20140213
      /// </summary>
      /// <param name="app_id"></param>
      /// <param name="firstname"></param>
      /// <param name="middlename"></param>
      /// <param name="surname"></param>
        public void GetContactIdByName(int app_id, string firstname,string middlename,string surname)
        {
            DataTable dt = null;
            try
            {
                dt=ApplicationDetailHandler.GetContactIdByName(app_id, firstname, middlename, surname);
                context.Response.Write(JsonConvert.SerializeObject(dt));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void combineApplication(int currentAppID, int ContactID, int combineAppID, string Operator)
        {
            try
            {
                context.Response.Write(ApplicationDetailHandler.combineApplication(currentAppID, ContactID, combineAppID, Operator));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void discombineApplication(int currentAppID, int ContactID, int combineAppID, string Operator)
        {
            try
            {
                context.Response.Write(ApplicationDetailHandler.disCombineApplication(currentAppID, ContactID, combineAppID, Operator));
            }
            catch (Exception ex)
            {
                context.Response.Write("1");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void LastResultData()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(ClientProfileHandler.BindLastResultData());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void OutSrcLender()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(ClientProfileHandler.BindOutSourceLender());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void ReferrerData()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(ClientProfileHandler.BindReferrerData());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        public void BindAwaitDocs()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(ClientProfileHandler.BindAwaitedDocs());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }

        }

        public void GetDistinctComesFrom()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(ApplicationDetailHandler.GetDistinctComesFrom());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }

        }

    }
}